
var $chatContainer = $('#chat_box_container');

pusher = new Pusher('2e92c25412a18c2f7f97', { authEndpoint: 'users/presence' }); //APP KEY
nettuts_channel = pusher.subscribe('presence-my-channel'); //join the presence-nettuts channel

pusher.connection.bind('connected', function() { //bind a function after we've connected to Pusher

	nettuts_channel.bind('pusher:subscription_succeeded', function(members) {
		members.each(function(member){
			addMember(member.info.data);
		}); 	
	});

	nettuts_channel.bind('pusher:member_added', function(member) {
	  	addMember(member.info.data);
	});

	nettuts_channel.bind('pusher:member_removed', function(member){
	  	removeMember(member.info.data);
	});
});

function updateTotalCount()
{
	$('#chat_online').text($('.chat_member').length);
}

function addMember(data)
{
	if(data.id != SS_USER_ID){
		var template = $('#onlineTpl').html();
		var html = Mustache.to_html(template,data);
		$chatContainer.append(html);
		updateTotalCount();
	}
}
function removeMember(data)
{
	$('#chat_id_' + data.id).fadeOut('slow');
	updateTotalCount();
}

